/**
 * 
 */
package com.dycn.posdashboard.services.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.time.DateUtils;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

import com.dycn.posdashboard.dao.PosTransactionDataDao;
import com.dycn.posdashboard.data.Branch2MerchantMapping;
import com.dycn.posdashboard.data.MerchantInfo;
import com.dycn.posdashboard.data.PaginatedInfor;
import com.dycn.posdashboard.data.PosTransactionData;
import com.dycn.posdashboard.data.PosTransactionDataStats;
import com.dycn.posdashboard.data.ServicesResultObject;
import com.dycn.posdashboard.data.StatsData001;
import com.dycn.posdashboard.data.StatsData002;
import com.dycn.posdashboard.data.StatsData003;
import com.dycn.posdashboard.data.StatsTimeUnit;
import com.dycn.posdashboard.services.CommonServices;
import com.dycn.posdashboard.services.PosTradeServices;
import com.dycn.posdashboard.util.DataCacheUtils;

/**
 * @author gongqiyi
 *
 */
public class PosTradeServicesImpl implements PosTradeServices {
	
	private PosTransactionDataDao posTransactionDataDao;
	private Cache systmDataCache;
	
	
	public Cache getSystmDataCache() {
		return systmDataCache;
	}

	public void setSystmDataCache(Cache systmDataCache) {
		this.systmDataCache = systmDataCache;
	}

	public PosTransactionDataDao getPosTransactionDataDao() {
		return posTransactionDataDao;
	}

	public void setPosTransactionDataDao(PosTransactionDataDao posTransactionDataDao) {
		this.posTransactionDataDao = posTransactionDataDao;
	}
	

	@Override
	public ServicesResultObject<PaginatedInfor<PosTransactionData>> getBranchPosTransactionDataListBy(
			String dbSource, String branchNo, Date startTime, Date endTime,
			PaginatedInfor<PosTransactionData> nonDataPaginatedInfor) {
		// TODO Auto-generated method stub
//		ServicesResultObject<PaginatedInfor<PosTransactionData>> someServicesResultObject=new ServicesResultObject<PaginatedInfor<PosTransactionData>>();
//		someServicesResultObject.setResultMsg("");
//		someServicesResultObject.setResultCode(CommonServices.RESULT_CODE_SUCCESS);
//		someServicesResultObject.setReturnObj(nonDataPaginatedInfor);
//		
//		int startIndex=(nonDataPaginatedInfor.getPageId()-1)*nonDataPaginatedInfor.getPageRecords()+1;
//		int endIndex=nonDataPaginatedInfor.getPageId()*nonDataPaginatedInfor.getPageRecords();
//		
//		try{
//			if(nonDataPaginatedInfor.getTotalRecords()<=0){
//				int totalRecords=this.posTransactionDataDao.getTotalRecordsOfPosTransactionDataListBy(dbSource, branchNo, null, null, startTime, endTime, startIndex, endIndex);
//				nonDataPaginatedInfor.setTotalRecords(totalRecords);
//			}
//			if(nonDataPaginatedInfor.getTotalRecords()>0){
//				List<PosTransactionData> posTransactionDataList=this.posTransactionDataDao.getPosTransactionDataListBy(dbSource, branchNo, null, null, startTime, endTime, startIndex, endIndex);
//				nonDataPaginatedInfor.setDataList(posTransactionDataList);
//			}
//		}catch(Exception exp){
//			someServicesResultObject.setResultMsg(exp.getMessage());
//			someServicesResultObject.setResultCode(CommonServices.RESULT_CODE_UNEXCEPTED_EXCEPTION);
//			someServicesResultObject.setReturnObj(null);
//		}
		
		//this.getPo(dbSource, startTime, endTime, nonDataPaginatedInfor)
		//return null;
		
		return getPosTransactionDataListBy(dbSource, branchNo, null, null, startTime, endTime, nonDataPaginatedInfor);
	}
	
	/**
	 * 
	 * @param dbSource
	 * @param branchNo
	 * @param merchantNo
	 * @param cardNo
	 * @param startTime
	 * @param endTime
	 * @param nonDataPaginatedInfor
	 * @return
	 */
	@Override
	public ServicesResultObject<PaginatedInfor<PosTransactionData>> getPosTransactionDataListBy(
			String dbSource, String branchNo,String merchantNo,String cardNo, Date startTime, Date endTime,
			PaginatedInfor<PosTransactionData> nonDataPaginatedInfor) {
		// TODO Auto-generated method stub
		ServicesResultObject<PaginatedInfor<PosTransactionData>> someServicesResultObject=new ServicesResultObject<PaginatedInfor<PosTransactionData>>();
		someServicesResultObject.setResultMsg("");
		someServicesResultObject.setResultCode(CommonServices.RESULT_CODE_SUCCESS);
		someServicesResultObject.setReturnObj(nonDataPaginatedInfor);
		
		int startIndex=(nonDataPaginatedInfor.getPageId()-1)*nonDataPaginatedInfor.getPageRecords()+1;
		int endIndex=nonDataPaginatedInfor.getPageId()*nonDataPaginatedInfor.getPageRecords();
		
		try{
			if(nonDataPaginatedInfor.getTotalRecords()<=0){
				int totalRecords=this.posTransactionDataDao.getTotalRecordsOfPosTransactionDataListBy(dbSource, branchNo, merchantNo, cardNo, startTime, endTime, startIndex, endIndex);
				nonDataPaginatedInfor.setTotalRecords(totalRecords);
			}

			List<PosTransactionData> posTransactionDataList=this.posTransactionDataDao.getPosTransactionDataListBy(dbSource, branchNo, merchantNo, cardNo, startTime, endTime, startIndex, endIndex);
			nonDataPaginatedInfor.setDataList(posTransactionDataList);

		}catch(Exception exp){
			exp.printStackTrace();
			someServicesResultObject.setResultMsg(exp.getMessage());
			someServicesResultObject.setResultCode(CommonServices.RESULT_CODE_UNEXCEPTED_EXCEPTION);
			someServicesResultObject.setReturnObj(null);
		}
		
		return someServicesResultObject;
	}
	
	@Override
	public ServicesResultObject<List<PosTransactionData>> getRecentPosTransactionDataListBy(
			String dbSource, String branchNo, String merchantNo, String cardNo,Date lastUpdateCheckStartTime,
			int recentSecond) {
		// TODO Auto-generated method stub
		ServicesResultObject<List<PosTransactionData>> someServicesResultObject=new ServicesResultObject<List<PosTransactionData>>();
		someServicesResultObject.setResultMsg("");
		someServicesResultObject.setResultCode(CommonServices.RESULT_CODE_SUCCESS);
		someServicesResultObject.setReturnObj(new ArrayList<PosTransactionData>());

		try{

			Date endTime=DateUtils.addSeconds(lastUpdateCheckStartTime,recentSecond);
			Date startTime=lastUpdateCheckStartTime;
			List<PosTransactionData> posTransactionDataList=this.posTransactionDataDao.getPosTransactionDataListBy(dbSource, branchNo, merchantNo, cardNo, startTime, endTime, -1, -1);
			someServicesResultObject.setReturnObj(posTransactionDataList);

		}catch(Exception exp){
			exp.printStackTrace();
			someServicesResultObject.setResultMsg(exp.getMessage());
			someServicesResultObject.setResultCode(CommonServices.RESULT_CODE_UNEXCEPTED_EXCEPTION);
			someServicesResultObject.setReturnObj(null);
		}
		
		return someServicesResultObject;
	}


	@Override
	public ServicesResultObject<PaginatedInfor<PosTransactionData>> getMerchantPosTransactionDataListBy(
			String dbSource, String merchantNo, Date startTime, Date endTime,
			PaginatedInfor<PosTransactionData> nonDataPaginatedInfor) {
		// TODO Auto-generated method stub
		return getPosTransactionDataListBy(dbSource, null, merchantNo, null, startTime, endTime, nonDataPaginatedInfor);
	}

	@Override
	public ServicesResultObject<PaginatedInfor<PosTransactionData>> getCardPosTransactionDataListBy(
			String dbSource, String cardNo, Date startTime, Date endTime,
			PaginatedInfor<PosTransactionData> nonDataPaginatedInfor) {
		// TODO Auto-generated method stub
		return getPosTransactionDataListBy(dbSource, null, null, cardNo, startTime, endTime, nonDataPaginatedInfor);
	}

	@Override
	public ServicesResultObject<PaginatedInfor<PosTransactionData>> getAllPosTransactionDataListBy(
			String dbSource, Date startTime, Date endTime,
			PaginatedInfor<PosTransactionData> nonDataPaginatedInfor) {
		// TODO Auto-generated method stub
		return getPosTransactionDataListBy(dbSource, null, null, null, startTime, endTime, nonDataPaginatedInfor);
	}

	@Override
	public ServicesResultObject<List<PosTransactionDataStats>> getMercantTradeMoenyOccupancy(
			String dbSource, String branchNo, Date startTime, Date endTime) {
		// TODO Auto-generated method stub
//		ServicesResultObject<List<PosTransactionDataStats>> someServicesResultObject=new ServicesResultObject<List<PosTransactionDataStats>>();
//		someServicesResultObject.setResultMsg("");
//		someServicesResultObject.setResultCode(CommonServices.RESULT_CODE_SUCCESS);
//		someServicesResultObject.setReturnObj(null);
//		
//		try{
//			List<PosTransactionDataStats> posTransactionDataStatsList=posTransactionDataDao.getPosTransactionDataStatsBy(dbSource, branchNo, null, null, startTime, endTime);
//			someServicesResultObject.setReturnObj(posTransactionDataStatsList);
//		}catch(Exception exp){
//			someServicesResultObject.setResultMsg(exp.getMessage());
//			someServicesResultObject.setResultCode(CommonServices.RESULT_CODE_UNEXCEPTED_EXCEPTION);
//			someServicesResultObject.setReturnObj(null);
//		}
		
		return getTradeMoneyOccupancy(dbSource,branchNo,null,null,startTime,endTime);
	}
	

	private ServicesResultObject<List<PosTransactionDataStats>> getTradeMoneyOccupancy(
			String dbSource, String branchNo, String merchantNo,String cardNo,Date startTime, Date endTime) {
		// TODO Auto-generated method stub
		ServicesResultObject<List<PosTransactionDataStats>> someServicesResultObject=new ServicesResultObject<List<PosTransactionDataStats>>();
		someServicesResultObject.setResultMsg("");
		someServicesResultObject.setResultCode(CommonServices.RESULT_CODE_SUCCESS);
		someServicesResultObject.setReturnObj(null);
		
		try{
			List<PosTransactionDataStats> posTransactionDataStatsList=posTransactionDataDao.getPosTransactionDataStatsBy(dbSource, branchNo, merchantNo, cardNo, startTime, endTime,-1,-1,StatsTimeUnit.DAY);
			someServicesResultObject.setReturnObj(posTransactionDataStatsList);
		}catch(Exception exp){
			someServicesResultObject.setResultMsg(exp.getMessage());
			someServicesResultObject.setResultCode(CommonServices.RESULT_CODE_UNEXCEPTED_EXCEPTION);
			someServicesResultObject.setReturnObj(null);
		}
		
		return someServicesResultObject;
	}

	@Override
	public ServicesResultObject<List<PosTransactionDataStats>> getBranchTradeMoenyOccupancy(
			String dbSource, Date startTime, Date endTime) {
		// TODO Auto-generated method stub
		return getTradeMoneyOccupancy(dbSource,null,null,null,startTime,endTime);
	}

	@Override
	public ServicesResultObject<List<PosTransactionDataStats>> getMercantAveragTradeMoeny(
			String dbSource, String branchNo, Date startTime, Date endTime,
			StatsTimeUnit statsTimeUnit) {
		// TODO Auto-generated method stub
		ServicesResultObject<List<PosTransactionDataStats>> someServicesResultObject=new ServicesResultObject<List<PosTransactionDataStats>>();
		someServicesResultObject.setResultMsg("");
		someServicesResultObject.setResultCode(CommonServices.RESULT_CODE_SUCCESS);
		someServicesResultObject.setReturnObj(null);
		
		try{
			List<PosTransactionDataStats> posTransactionDataStatsList=posTransactionDataDao.getPosTransactionDataStatsBy(dbSource, branchNo, null, null, startTime, endTime,-1,-1,statsTimeUnit);
			someServicesResultObject.setReturnObj(posTransactionDataStatsList);
		}catch(Exception exp){
			someServicesResultObject.setResultMsg(exp.getMessage());
			someServicesResultObject.setResultCode(CommonServices.RESULT_CODE_UNEXCEPTED_EXCEPTION);
			someServicesResultObject.setReturnObj(null);
		}
		
		return someServicesResultObject;
	}

	@Override
	public ServicesResultObject<List<PosTransactionDataStats>> getCardOwnerAveragTradeMoeny(
			String dbSource, Date startTime, Date endTime,
			StatsTimeUnit statsTimeUnit) {
		// TODO Auto-generated method stub
		ServicesResultObject<List<PosTransactionDataStats>> someServicesResultObject=new ServicesResultObject<List<PosTransactionDataStats>>();
		someServicesResultObject.setResultMsg("");
		someServicesResultObject.setResultCode(CommonServices.RESULT_CODE_SUCCESS);
		someServicesResultObject.setReturnObj(null);
		
		try{
			List<PosTransactionDataStats> posTransactionDataStatsList=posTransactionDataDao.getPosTransactionDataStatsBy(dbSource, null,null,null,startTime, endTime,-1,-1,statsTimeUnit);
			someServicesResultObject.setReturnObj(posTransactionDataStatsList);
		}catch(Exception exp){
			someServicesResultObject.setResultMsg(exp.getMessage());
			someServicesResultObject.setResultCode(CommonServices.RESULT_CODE_UNEXCEPTED_EXCEPTION);
			someServicesResultObject.setReturnObj(null);
		}
		
		return someServicesResultObject;
	}

	@Override
	public ServicesResultObject<List<PosTransactionDataStats>> getBranchTradeMoenyOccupancy(
			String dbSource, Date startTime, Date endTime,
			StatsTimeUnit statsTimeUnit) {
		// TODO Auto-generated method stub
		ServicesResultObject<List<PosTransactionDataStats>> someServicesResultObject=new ServicesResultObject<List<PosTransactionDataStats>>();
		someServicesResultObject.setResultMsg("");
		someServicesResultObject.setResultCode(CommonServices.RESULT_CODE_SUCCESS);
		someServicesResultObject.setReturnObj(null);
		
		try{
			List<PosTransactionDataStats> posTransactionDataStatsList=posTransactionDataDao.getPosTransactionDataStatsBy(dbSource,null,null,null,startTime, endTime,-1,-1,statsTimeUnit);
			someServicesResultObject.setReturnObj(posTransactionDataStatsList);
		}catch(Exception exp){
			someServicesResultObject.setResultMsg(exp.getMessage());
			someServicesResultObject.setResultCode(CommonServices.RESULT_CODE_UNEXCEPTED_EXCEPTION);
			someServicesResultObject.setReturnObj(null);
		}
		
		return someServicesResultObject;
	}

	@Override
	public ServicesResultObject<List<PosTransactionDataStats>> getMerchantTradeMoenyOccupancy(
			String dbSource, String branchNo, Date startTime, Date endTime,
			StatsTimeUnit statsTimeUnit) {
		// TODO Auto-generated method stub
		ServicesResultObject<List<PosTransactionDataStats>> someServicesResultObject=new ServicesResultObject<List<PosTransactionDataStats>>();
		someServicesResultObject.setResultMsg("");
		someServicesResultObject.setResultCode(CommonServices.RESULT_CODE_SUCCESS);
		someServicesResultObject.setReturnObj(null);
		
		try{
			List<PosTransactionDataStats> posTransactionDataStatsList=posTransactionDataDao.getPosTransactionDataStatsBy(dbSource,branchNo,null,null, startTime, endTime,-1,-1,statsTimeUnit);
			someServicesResultObject.setReturnObj(posTransactionDataStatsList);
		}catch(Exception exp){
			someServicesResultObject.setResultMsg(exp.getMessage());
			someServicesResultObject.setResultCode(CommonServices.RESULT_CODE_UNEXCEPTED_EXCEPTION);
			someServicesResultObject.setReturnObj(null);
		}
		
		return someServicesResultObject;
	}

	@Override
	public ServicesResultObject<Branch2MerchantMapping> getBranch2MerchantMappingInfo(String dbSource) {
		// TODO Auto-generated method stub
		ServicesResultObject<Branch2MerchantMapping> someServicesResultObject=new ServicesResultObject<Branch2MerchantMapping>();
		someServicesResultObject.setResultMsg("");
		someServicesResultObject.setResultCode(CommonServices.RESULT_CODE_SUCCESS);
		someServicesResultObject.setReturnObj(null);
		
		Element branchMerchantMappingElement=systmDataCache.get(DataCacheUtils.BRANCH_2_MERCHANT_MAPPING);
		if(branchMerchantMappingElement==null){
			List<MerchantInfo> branch2MerchantMappingList=this.posTransactionDataDao.getBranch2MerchantMapping(dbSource);
			Branch2MerchantMapping someBranch2MerchantMapping=new Branch2MerchantMapping();
			
			someBranch2MerchantMapping.build(branch2MerchantMappingList);
//			Map<String,Branch2MerchantMapping> branch2MerchantMapping=new HashMap<String,Branch2MerchantMapping>();
//			for(Branch2MerchantMapping someBranch2MerchantMapping:branch2MerchantMappingList){
//				branch2MerchantMapping.put(someBranch2MerchantMapping.getBranch().getBranchNo(), someBranch2MerchantMapping);
//			}
			branchMerchantMappingElement=new Element(DataCacheUtils.BRANCH_2_MERCHANT_MAPPING,someBranch2MerchantMapping);
			branchMerchantMappingElement.setTimeToIdle(120);//60秒后过期
			systmDataCache.put(branchMerchantMappingElement);
		}
		Branch2MerchantMapping branch2MerchantMapping=(Branch2MerchantMapping)branchMerchantMappingElement.getValue();
		someServicesResultObject.setReturnObj(branch2MerchantMapping);
		
		return someServicesResultObject;
	}


	
	/*
	 * (non-Javadoc)
	 * @see com.dycn.posdashboard.services.PosTradeServices#getPosTradeStatsByCardNo(java.lang.String, java.util.Date, java.util.Date, com.dycn.posdashboard.data.StatsTimeUnit)
	 */
	@Override
	public ServicesResultObject<PaginatedInfor<PosTransactionDataStats>> getPosTradeStatsByCardNo(
			String dbSource, Date startTime, Date endTime,StatsTimeUnit statsTimeUnit,
			PaginatedInfor<PosTransactionDataStats> nonDataPaginatedInfor) {
		// TODO Auto-generated method stub
		ServicesResultObject<PaginatedInfor<PosTransactionDataStats>> someServicesResultObject=new ServicesResultObject<PaginatedInfor<PosTransactionDataStats>>();
		someServicesResultObject.setResultMsg("");
		someServicesResultObject.setResultCode(CommonServices.RESULT_CODE_SUCCESS);
		someServicesResultObject.setReturnObj(null);
		
		try{
			int startIndex=(nonDataPaginatedInfor.getPageId()-1)*nonDataPaginatedInfor.getPageRecords()+1;
			int endIndex=nonDataPaginatedInfor.getPageId()*nonDataPaginatedInfor.getPageRecords();
			
			if(nonDataPaginatedInfor.getTotalRecords()<=0){
				//
				int totalRecords=posTransactionDataDao.getTotalRecordsOfPosTransactionDataStatsByy(dbSource, null, null, null, startTime, endTime,statsTimeUnit);
				nonDataPaginatedInfor.setTotalRecords(totalRecords);
			}
				
			List<PosTransactionDataStats> posTransactionDataStatsList=posTransactionDataDao.getPosTransactionDataStatsBy(dbSource,null,null,null, startTime, endTime,startIndex,endIndex,statsTimeUnit);
			nonDataPaginatedInfor.setDataList(posTransactionDataStatsList);
			
			someServicesResultObject.setReturnObj(nonDataPaginatedInfor);
			
		}catch(Exception exp){
			exp.printStackTrace();
			someServicesResultObject.setResultMsg(exp.getMessage());
			someServicesResultObject.setResultCode(CommonServices.RESULT_CODE_UNEXCEPTED_EXCEPTION);
			someServicesResultObject.setReturnObj(null);
		}
		
		return someServicesResultObject;
	}

	/*
	 * 	(non-Javadoc)
	 * @see com.dycn.posdashboard.services.PosTradeServices#getPosTradeStatsByTerminalMerchantNo(java.lang.String, java.lang.String, java.util.Date, java.util.Date, com.dycn.posdashboard.data.PaginatedInfor)
	 */
	@Override
	public ServicesResultObject<PaginatedInfor<PosTransactionDataStats>> getPosTradeStatsByTerminalMerchantNo(
			String dbSource, String terminalMerchantNo, Date startTime,
			Date endTime,
			PaginatedInfor<PosTransactionDataStats> nonDataPaginatedInfor) {
		// TODO Auto-generated method stub
		ServicesResultObject<PaginatedInfor<PosTransactionDataStats>> someServicesResultObject=new ServicesResultObject<PaginatedInfor<PosTransactionDataStats>>();
		someServicesResultObject.setResultMsg("");
		someServicesResultObject.setResultCode(CommonServices.RESULT_CODE_SUCCESS);
		someServicesResultObject.setReturnObj(null);
		
		try{
			int startIndex=(nonDataPaginatedInfor.getPageId()-1)*nonDataPaginatedInfor.getPageRecords()+1;
			int endIndex=nonDataPaginatedInfor.getPageId()*nonDataPaginatedInfor.getPageRecords();
			
			if(nonDataPaginatedInfor.getTotalRecords()<=0){
				//
				int totalRecords=posTransactionDataDao.getTotalRecordsOfPosTransactionDataStatsByy(dbSource, null, terminalMerchantNo, null, startTime, endTime,null);
				nonDataPaginatedInfor.setTotalRecords(totalRecords);
			}
				
			List<PosTransactionDataStats> posTransactionDataStatsList=posTransactionDataDao.getPosTransactionDataStatsBy(dbSource,null,terminalMerchantNo,null, startTime, endTime,startIndex,endIndex,null);
			nonDataPaginatedInfor.setDataList(posTransactionDataStatsList);
			
			someServicesResultObject.setReturnObj(nonDataPaginatedInfor);
			
		}catch(Exception exp){
			exp.printStackTrace();
			someServicesResultObject.setResultMsg(exp.getMessage());
			someServicesResultObject.setResultCode(CommonServices.RESULT_CODE_UNEXCEPTED_EXCEPTION);
			someServicesResultObject.setReturnObj(null);
		}
		
		return someServicesResultObject;
	}

	public ServicesResultObject<PaginatedInfor<PosTransactionDataStats>> getMerchantPosTradeStatsByBranch(String dbSource,String branchNo,Date startTime,Date endTime,PaginatedInfor<PosTransactionDataStats> nonDataPaginatedInfor){
		ServicesResultObject<PaginatedInfor<PosTransactionDataStats>> someServicesResultObject=new ServicesResultObject<PaginatedInfor<PosTransactionDataStats>>();
		someServicesResultObject.setResultMsg("");
		someServicesResultObject.setResultCode(CommonServices.RESULT_CODE_SUCCESS);
		someServicesResultObject.setReturnObj(null);
		
		try{
			int startIndex=(nonDataPaginatedInfor.getPageId()-1)*nonDataPaginatedInfor.getPageRecords()+1;
			int endIndex=nonDataPaginatedInfor.getPageId()*nonDataPaginatedInfor.getPageRecords();
			
			if(nonDataPaginatedInfor.getTotalRecords()<=0){
				//
				int totalRecords=posTransactionDataDao.getTotalRecordsOfMerchantPosTransactionDataStatsByyBranch(dbSource, branchNo,startTime, endTime);
				nonDataPaginatedInfor.setTotalRecords(totalRecords);
			}
				
			List<PosTransactionDataStats> posTransactionDataStatsList=posTransactionDataDao.getMerchantPosTransactionDataStatsByBranch(dbSource,branchNo, startTime, endTime,startIndex,endIndex);
			nonDataPaginatedInfor.setDataList(posTransactionDataStatsList);
			
			someServicesResultObject.setReturnObj(nonDataPaginatedInfor);
			
		}catch(Exception exp){
			exp.printStackTrace();
			someServicesResultObject.setResultMsg(exp.getMessage());
			someServicesResultObject.setResultCode(CommonServices.RESULT_CODE_UNEXCEPTED_EXCEPTION);
			someServicesResultObject.setReturnObj(null);
		}
		
		return someServicesResultObject;
	}

	/*
	 * (non-Javadoc)
	 * @see com.dycn.posdashboard.services.PosTradeServices#getPosTradeStatsByBranch(java.lang.String, java.lang.String, java.util.Date, java.util.Date, com.dycn.posdashboard.data.PaginatedInfor)
	 */
	@Override
	public ServicesResultObject<PaginatedInfor<PosTransactionDataStats>> getPosTradeStatsByBranch(
			String dbSource, String branchNo, Date startTime, Date endTime,
			PaginatedInfor<PosTransactionDataStats> nonDataPaginatedInfor) {
		// TODO Auto-generated method stub
		ServicesResultObject<PaginatedInfor<PosTransactionDataStats>> someServicesResultObject=new ServicesResultObject<PaginatedInfor<PosTransactionDataStats>>();
		someServicesResultObject.setResultMsg("");
		someServicesResultObject.setResultCode(CommonServices.RESULT_CODE_SUCCESS);
		someServicesResultObject.setReturnObj(null);
		
		try{
			int startIndex=(nonDataPaginatedInfor.getPageId()-1)*nonDataPaginatedInfor.getPageRecords()+1;
			int endIndex=nonDataPaginatedInfor.getPageId()*nonDataPaginatedInfor.getPageRecords();
			
			if(nonDataPaginatedInfor.getTotalRecords()<=0){
				//
				int totalRecords=posTransactionDataDao.getTotalRecordsOfPosTransactionDataStatsByyBranch(dbSource, branchNo,startTime, endTime);
				nonDataPaginatedInfor.setTotalRecords(totalRecords);
			}
				
			List<PosTransactionDataStats> posTransactionDataStatsList=posTransactionDataDao.getPosTransactionDataStatsByBranch(dbSource,branchNo, startTime, endTime,startIndex,endIndex);
			nonDataPaginatedInfor.setDataList(posTransactionDataStatsList);
			
			someServicesResultObject.setReturnObj(nonDataPaginatedInfor);
			
		}catch(Exception exp){
			exp.printStackTrace();
			someServicesResultObject.setResultMsg(exp.getMessage());
			someServicesResultObject.setResultCode(CommonServices.RESULT_CODE_UNEXCEPTED_EXCEPTION);
			someServicesResultObject.setReturnObj(null);
		}
		
		return someServicesResultObject;
	}

	
	@Override
	public ServicesResultObject<List<StatsData001>> queryStatsData001(
			String dbSource, String branchNo, String terminalMerchantNo,
			Date startTime, Date endTime) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		ServicesResultObject<List<StatsData001>> someServicesResultObject=new ServicesResultObject<List<StatsData001>>();
		someServicesResultObject.setResultMsg("");
		someServicesResultObject.setResultCode(CommonServices.RESULT_CODE_SUCCESS);
		someServicesResultObject.setReturnObj(null);
		
		try{

			List<StatsData001> posTransactionDataStatsList=posTransactionDataDao.getStatsData001(dbSource,branchNo,terminalMerchantNo,null, startTime, endTime);

			someServicesResultObject.setReturnObj(posTransactionDataStatsList);
			
		}catch(Exception exp){
			exp.printStackTrace();
			someServicesResultObject.setResultMsg(exp.getMessage());
			someServicesResultObject.setResultCode(CommonServices.RESULT_CODE_UNEXCEPTED_EXCEPTION);
			someServicesResultObject.setReturnObj(null);
		}
		
		return someServicesResultObject;
	}
	
	@Override
	public ServicesResultObject<List<StatsData001>> queryStatsData(
			String dbSource, String branchNo, String terminalMerchantNo,
			Date startTime, Date endTime,StatsTimeUnit stateTimeUnit) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		ServicesResultObject<List<StatsData001>> someServicesResultObject=new ServicesResultObject<List<StatsData001>>();
		someServicesResultObject.setResultMsg("");
		someServicesResultObject.setResultCode(CommonServices.RESULT_CODE_SUCCESS);
		someServicesResultObject.setReturnObj(null);
		
		try{
			List<StatsData001> posTransactionDataStatsList=null;
			if(stateTimeUnit==StatsTimeUnit.DAY){
				posTransactionDataStatsList=posTransactionDataDao.getStatsData001(dbSource,branchNo,terminalMerchantNo,null, startTime, endTime);

			}else if(stateTimeUnit==StatsTimeUnit.MONTH){
				posTransactionDataStatsList=posTransactionDataDao.getStatsData001Month(dbSource,branchNo,terminalMerchantNo,null, startTime, endTime);

			}else if(stateTimeUnit==StatsTimeUnit.YEAR){
				posTransactionDataStatsList=posTransactionDataDao.getStatsData001Year(dbSource,branchNo,terminalMerchantNo,null, startTime, endTime);

			}else{
				posTransactionDataStatsList=posTransactionDataDao.getStatsData001(dbSource,branchNo,terminalMerchantNo,null, startTime, endTime);

			}
	
			someServicesResultObject.setReturnObj(posTransactionDataStatsList);
			
		}catch(Exception exp){
			exp.printStackTrace();
			someServicesResultObject.setResultMsg(exp.getMessage());
			someServicesResultObject.setResultCode(CommonServices.RESULT_CODE_UNEXCEPTED_EXCEPTION);
			someServicesResultObject.setReturnObj(null);
		}
		
		return someServicesResultObject;
	}

	@Override
	public ServicesResultObject<List<StatsData002>> queryStatsData002(
			String dbSource, String branchNo, String terminalMerchantNo,
			Date startTime, Date endTime) {
		// TODO Auto-generated method stub
		ServicesResultObject<List<StatsData002>> someServicesResultObject=new ServicesResultObject<List<StatsData002>>();
		someServicesResultObject.setResultMsg("");
		someServicesResultObject.setResultCode(CommonServices.RESULT_CODE_SUCCESS);
		someServicesResultObject.setReturnObj(null);
		
		try{

			List<StatsData002> posTransactionDataStatsList=posTransactionDataDao.getStatsData002(dbSource,branchNo,terminalMerchantNo,null, startTime, endTime);

			someServicesResultObject.setReturnObj(posTransactionDataStatsList);
			
		}catch(Exception exp){
			exp.printStackTrace();
			someServicesResultObject.setResultMsg(exp.getMessage());
			someServicesResultObject.setResultCode(CommonServices.RESULT_CODE_UNEXCEPTED_EXCEPTION);
			someServicesResultObject.setReturnObj(null);
		}
		
		return someServicesResultObject;
	}
	
	@Override
	public ServicesResultObject<List<StatsData003>> queryStatsData003(
			String dbSource, String branchNo, String terminalMerchantNo,
			Date startTime, Date endTime) {
		// TODO Auto-generated method stub
		ServicesResultObject<List<StatsData003>> someServicesResultObject=new ServicesResultObject<List<StatsData003>>();
		someServicesResultObject.setResultMsg("");
		someServicesResultObject.setResultCode(CommonServices.RESULT_CODE_SUCCESS);
		someServicesResultObject.setReturnObj(null);
		
		try{

			List<StatsData003> posTransactionDataStatsList=posTransactionDataDao.getStatsData003(dbSource,branchNo,terminalMerchantNo,null, startTime, endTime);

			someServicesResultObject.setReturnObj(posTransactionDataStatsList);
			
		}catch(Exception exp){
			exp.printStackTrace();
			someServicesResultObject.setResultMsg(exp.getMessage());
			someServicesResultObject.setResultCode(CommonServices.RESULT_CODE_UNEXCEPTED_EXCEPTION);
			someServicesResultObject.setReturnObj(null);
		}
		
		return someServicesResultObject;
	}


}
